package com.otaku.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.otaku.admin.annotation.PageCheck;
import com.otaku.admin.dao.entity.Permission;
import com.otaku.admin.dao.entity.Role;
import com.otaku.admin.dao.entity.RolePermission;
import com.otaku.admin.dao.entity.UserRole;
import com.otaku.admin.service.PermissionService;
import com.otaku.admin.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 用户类
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    private PermissionService permissionService;
    @Autowired
    private RoleService roleService;

    /**
     * 新增权限
     *
     * @return
     */
    @PostMapping("/addPerm")
    public String insertPermission(Permission permission) {
        boolean b = permissionService.addPerm(permission);
        return b ? responseSuccessJSON() : responseErrorJSON("保存失败");
    }

    /**
     * 删除权限
     */
    @GetMapping("/deletePerm")
    public String deletePermission(Permission permission) {
        boolean b = permissionService.deletePerm(permission);
        return b ? responseSuccessJSON() : responseErrorJSON("保存失败");
    }

    /**
     * 更新权限
     *
     * @param permission
     * @return
     */
    @PostMapping("/updatePerm")
    public String updatePermission(Permission permission) {
        boolean b = permissionService.updatePerm(permission);
        return b ? responseSuccessJSON() : responseErrorJSON("保存失败");
    }

    /**
     * 查询全部权限
     *
     * @return
     */
    @GetMapping("/selectPerm")
    public String selectPermissionAll() {
        List<Permission> permAll = permissionService.findPermAll();
        return responseSuccessJSON(permAll);
    }

    /**
     * 查询用户权限
     *
     * @return
     */
    @GetMapping("/selectPermByUser")
    public String selectPermissionByUser() {
        String userNameFromToken = getUserNameFromToken();
        List<Permission> permByUser = permissionService.findPermByUser(userNameFromToken);
        return responseSuccessJSON(permByUser);
    }

    /**
     * 新增角色类型
     *
     * @param role
     * @return
     */
    @PostMapping("/insertRole")
    public String insertRole(Role role) {
        boolean b = roleService.addRole(role);
        return b ? responseSuccessJSON() : responseErrorJSON("保存失败");
    }

    /**
     * 删除角色
     *
     * @param role
     * @return
     */
    @GetMapping("/deleteRole")
    public String deleteRole(Role role) {
        boolean b = roleService.deleteRole(role);
        return b ? responseSuccessJSON() : responseErrorJSON("保存失败");
    }

    /**
     * 更新角色
     *
     * @param role
     * @return
     */
    @PostMapping("/upDateRole")
    public String upDateRole(Role role) {
        boolean b = roleService.updateRole(role);
        return b ? responseSuccessJSON() : responseErrorJSON("保存失败");
    }

    /**
     * 查询全部角色
     *
     * @return
     */
    @GetMapping("/selectRole")
    public String selectRole() {
        List<Role> roles = roleService.selectRoleAll();
        return responseSuccessJSON(roles);
    }

    /**
     *
     * @param role
     * @return
     */
    @PageCheck(params = {"page.current","page.size"})
    @GetMapping("/selectRoleList")
    public String selectRoleList(Role role) {
        IPage<Role> all = roleService.findAll(role);
        return responseSuccessJSON(all);
    }

    /**
     * 查询该用户的角色
     *
     * @return
     */
    @GetMapping("/selectRoleByUser")
    public String selectRoleByUser() {
        String userNameFromToken = getUserNameFromToken();
        List<Role> roles = roleService.selectRoleByUser(userNameFromToken);
        return responseSuccessJSON(roles);
    }

    /**
     * 为用户添加角色
     *
     * @param userRole roleId
     * @return
     */
    @PostMapping("/addUserRole")
    public String addUserRole(UserRole userRole) {
        String userNameFromToken = getUserNameFromToken();
        boolean b = permissionService.addUserRole(userRole, userNameFromToken);
        return b ? responseSuccessJSON() : responseErrorJSON("保存失败");
    }

    /**
     * 为用户删除角色
     *
     * @param userRole roleId
     * @return
     */
    @PostMapping("/deleteUserRole")
    public String deleteUserRole(UserRole userRole) {
        String userNameFromToken = getUserNameFromToken();
        permissionService.deleteUserRole(userRole, userNameFromToken);
        return responseSuccessJSON();
    }

    /**
     * 为角色分配权限
     */
    @PostMapping("/addRolePerm")
    public String addRolePerm(RolePermission rolePermission) {
        permissionService.addRolePerm(rolePermission);
        return responseSuccessJSON();
    }

    /**
     * 删除角色分配的权限
     */
    @PostMapping("/deleteRolePerm")
    public String deleteRolePerm(RolePermission rolePermission) {
        permissionService.deleteRolePerm(rolePermission);
        return responseSuccessJSON();
    }

}
